
	
// dbhost = 'edb_a99999'
// appkey = 'c184567b'
// appscret = '90353b57f17a4bf6a11263f0545ddbdc'
// token = 'e6513e432b724720ae6b6ab4155e6ccb'
var md5 = require('md5')
var request = require('request')
var moment = require('moment')
var request = require('request')
// var urlencode = require('urlencode')
var shopid = String(28)
var localip = '192.168.1.153'
var edburl = 'http://vm3008.edb01.com/rest/index.aspx'

var appkey = '6f73265f'
var appscret = '66b694959a634086a481b8fab66f3ffa'
var token = '176821cc17da44989d4cc697e341bf0f'
var dbhost = 'edb_a87822'

// dbhost = 'edb_a99999'
// appkey = 'c184567b'
// appscret = '90353b57f17a4bf6a11263f0545ddbdc'
// token = 'e6513e432b724720ae6b6ab4155e6ccb'

module.exports.addEDB = function addEDB(xml,method,callback){
//	edburl = 'http://qimen.6x86.net:10537/restxin/index.aspx'
	var params = {}
	params['method'] = method
	params['dbhost'] = dbhost
	params['appkey'] = appkey
	params['appscret'] = appscret
	params['format'] = 'JSON'
	params['timestamp'] = moment(new Date()).utcOffset(+480).format('YYYYMMDDHHmm')
	 console.log( moment(new Date()).format('YYYYMMDDHHmm'),params['timestamp'])
	// params['timestamp'] = '201807191452'
	params['v'] = '2.0';
    params['slencry'] = '1';
    params['ip'] = localip;
    params['token'] = token
    params['xmlValues'] = xml
     var keys = Object.keys(params).sort(function (a, b) {
    return a.toLowerCase().localeCompare(b.toLowerCase());
});
    var paramsStr = appkey
    for (var i = 0 ; i < keys.length;i++){
    	
    	var x = keys[i]
    	paramsStr += x + params[x]
    }
    var signature = md5(paramsStr).toUpperCase()
    params['sign'] = signature
    var query = ''
    delete params['token']
    delete params['appscret']

    for (var i = 0 ; i < Object.keys(params).length;i++){
    	var tempsad = ''
    	if (Object.keys(params)[i] == 'xmlValues'){
    		tempsad = urlencodex(params['xmlValues'])
    		//console.log(tempsad)
    		//tempsad = ''
    	}
    	else{
    		tempsad = params[Object.keys(params)[i]]
    	}
    	query += Object.keys(params)[i] + '=' + tempsad
    	query += (i != Object.keys(params).length - 1) ? '&' : ''   
    }

    request({
		 	url: edburl,
		 	method: "GET",
		 	timeout: 10000,
		 	followRedirect: true,
		 	maxRedirects: 10,
		 	form : query,
		 	headers : {
		 	'Content-Type': 'application/x-www-form-urlencodedrn',
		 	'Content-Length' : Buffer.byteLength(query)
		 	}
		 },function(error, response, body){
			//console.log(error,'hey eroro')
		 	//console.log(response,'jey ')
		 	callback(error, response, body)

	})


}


module.exports.generateEDBxml = function generateEDBxml(openid,order,prepay_id){
	 var orderid = Math.round(Math.random(1) * 100000000000)

	var xml = '<order><orderInfo>'
	xml += '<out_tid>' + String(order._id) + '</out_tid>'
	xml += '<shop_id>' + String(shopid) + '</shop_id>'
	xml += '<storage_id>2</storage_id>'
	xml += '<buyer_id>' + openid + '</buyer_id>'
	xml += '<buyer_msg>' + '小程序订单' + '</buyer_msg>'
	xml += '<consignee>' + order.address.userName + '</consignee>'
	xml += '<mobilPhone>' + order.address.telNumber + '</mobilPhone>'
	xml += '<privince>' + order.address.provinceName + '</privince>'
	xml += '<city>' + order.address.cityName + '</city>'
	xml += '<area>' + order.address.countyName +'</area>'
	xml += '<address><![CDATA[' + order.address.provinceName + order.address.cityName + order.address.countyName + order.address.detailInfo + ']]></address>'
	//xml += '<address><![CDATA[广东省深圳宝安区龙华富士康北大门（园区内J2会员中心）]]></address>'
	xml += '<actual_freight_get>' + String(order.shippingFee) + '</actual_freight_get>'
	xml += '<is_COD>0</is_COD>' 
	xml += '<order_totalMoney>'  + String(order.totalPrice) + '</order_totalMoney>'
	xml += '<product_totalMoney>'  + String(order.productPrice) + '</product_totalMoney>'
	xml += '<favorable_money>'  + String(order.couponPrice) + '</favorable_money>'
	xml += '<pay_method>'  + '微信支付' + '</pay_method>'
	xml += '<out_payNo>' + 'prepay_id' + '</out_payNo>'
	xml += '<pay_date>' + moment(new Date()).utcOffset(+480).format('YYYY-MM-DD HH:mm:ss') + '</pay_date>'
	xml += '<order_date>' + moment(new Date()).utcOffset(+480).format('YYYY-MM-DD HH:mm:ss') + '</order_date>'
	xml += '<terminal_type>' + '手机' + '</terminal_type>'
	xml += '<pay_status>' + '已付款' + '</pay_status>'
	xml += '</orderInfo>'
	xml += '<product_info>'
	 for (var i = 0 ; i < order.products.length;i++){
	 	xml += '<product_item>'
	 	xml += '<barCode>' + order.products[i].selectedSN + '</barCode>'
	 	xml += '<product_title>' + order.products[i].name + '</product_title>'
	 	xml += '<orderGoods_Num>' + String(order.products[i].selectedNumber) + '</orderGoods_Num>'
	 	xml += '<standard>' + (order.products[i].selectedAttribute != undefined) ? order.products[i].selectedAttribute  + '  X ' + String(order.products[i].selectedNumber) : 'X ' + String(order.products[i].selectedNumber) + '</standard>'
	 	xml += '<cost_Price>' + String(parseInt(order.products[i].selectedNumber) * parseFloat(order.products[i].itemPrice)) + '</cost_Price>'
	 	xml += '<out_tid>' + String(order._id) + '</out_tid>'
	 	xml += '</product_item>'
	 }
	xml += '</product_info>' + '</order>'

		

	return xml

}



function formatDate1(date){


}

 function urlencodex (str) {


  str = (str + '')

  // Tilde should be allowed unescaped in future versions of PHP (as reflected below),
  // but if you want to reflect current
  // PHP behavior, you would need to add ".replace(/~/g, '%7E');" to the following.
  return encodeURIComponent(str)
    .replace(/!/g, '%21')
    .replace(/'/g, '%27')
    .replace(/\(/g, '%28')
    .replace(/\)/g, '%29')
    .replace(/\*/g, '%2A')
    .replace(/%20/g, '+')
}